package com.shaoshaohuo.app.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.FarmDownUpShop;
import com.shaoshaohuo.app.entity.FarmMyShop;
import com.shaoshaohuo.app.net.ShopHttpConfig;
import com.shaoshaohuo.app.utils.Okhttputils;
import com.shaoshaohuo.app.view.MyImageView;
import com.shaoshaohuo.app.view.TopbarView;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/6/29.
 */

public class MyFarmShopAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private List<FarmMyShop.DataBean> dataBeen;

    public MyFarmShopAdapter(Context context, List<FarmMyShop.DataBean> dataBeen) {
        this.context = context;
        this.dataBeen = dataBeen;
    }

    private Handler handler = new Handler();

    public void refreshAdapter(List<FarmMyShop.DataBean> dataBeen) {
        this.dataBeen = dataBeen;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return dataBeen != null ? dataBeen.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return dataBeen.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        farmMyShopViewHolder holder = null;
        if (convertView == null) {

            convertView = LayoutInflater.from(context).inflate(R.layout.my_farm_shop_item, parent, false);
            holder = new farmMyShopViewHolder();
            holder.my_farm_shop_item_collect = (TextView) convertView.findViewById(R.id.my_farm_shop_item_collect);
            holder.my_farm_shop_item_date = (TextView) convertView.findViewById(R.id.my_farm_shop_item_date);
            holder.my_farm_shop_item_name = (TextView) convertView.findViewById(R.id.my_farm_shop_item_name);
            holder.my_farm_shop_item_goumai = (TextView) convertView.findViewById(R.id.my_farm_shop_item_goumai);
            holder.my_farm_shop_item_look = (TextView) convertView.findViewById(R.id.my_farm_shop_item_look);
            holder.my_farm_shop_item_remark = (TextView) convertView.findViewById(R.id.my_farm_shop_item_remark);
            holder.my_farm_shop_item_cancel = (TextView) convertView.findViewById(R.id.my_farm_shop_item_cancel);
            holder.my_farm_shop_item_hint = (TextView) convertView.findViewById(R.id.my_farm_shop_item_hint);
            holder.my_farm_shop_item_imageView = (MyImageView) convertView.findViewById(R.id.my_farm_shop_item_imageView);
            holder.my_farm_shop_item_bianji = (ImageView) convertView.findViewById(R.id.my_farm_shop_item_bianji);
            convertView.setTag(holder);
        } else {
            holder = (farmMyShopViewHolder) convertView.getTag();
        }
        if (dataBeen.get(position).getStatus().equals("3")) {
            //3是上架2是下架
            holder.my_farm_shop_item_cancel.setVisibility(View.INVISIBLE);
            holder.my_farm_shop_item_hint.setVisibility(View.GONE);
        } else if (dataBeen.get(position).getStatus().equals("2")) {
            holder.my_farm_shop_item_cancel.setVisibility(View.INVISIBLE);
            holder.my_farm_shop_item_hint.setVisibility(View.VISIBLE);
        }
        Picasso.with(context).load(dataBeen.get(position).getImg()).into(holder.my_farm_shop_item_imageView);
        holder.my_farm_shop_item_date.setText(dataBeen.get(position).getPushTime());
        holder.my_farm_shop_item_name.setText(dataBeen.get(position).getProName());
        holder.my_farm_shop_item_collect.setText(dataBeen.get(position).getCollection() + "");
        holder.my_farm_shop_item_goumai.setText(dataBeen.get(position).getPayNum() + "");
        holder.my_farm_shop_item_look.setText(dataBeen.get(position).getScanNum() + "");
        holder.my_farm_shop_item_remark.setText(dataBeen.get(position).getRemarkNum() + "");
        holder.my_farm_shop_item_bianji.setTag(position);
        holder.my_farm_shop_item_bianji.setOnClickListener(this);
        return convertView;
    }

    private DownUpCallBack downUpCallBack;

    public void setDownUpCallBack(DownUpCallBack downUpCallBack) {
        this.downUpCallBack = downUpCallBack;
    }

    @Override
    public void onClick(View v) {
        final int position = (int) v.getTag();
        switch (v.getId()) {
            case R.id.my_farm_shop_item_bianji:
                final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
                if (dataBeen.get(position).getStatus().equals("2")) {
                    alertDialog.setTitle("是否上架商品");
                } else if (dataBeen.get(position).getStatus().equals("3")) {
                    alertDialog.setTitle("是否下架商品");
                }
                alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        downUpShop("1", dataBeen.get(position).getCatid());
                        if (dataBeen.get(position).getStatus().equals("2")) {
                            downUpCallBack.callBack("2");
                        } else if (dataBeen.get(position).getStatus().equals("3")) {
                            downUpCallBack.callBack("1");
                        }

                    }
                });
                alertDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.create().dismiss();
                    }
                });
                alertDialog.setCancelable(false);
                alertDialog.create().show();
                break;
        }
    }

    private void downUpShop(String type, String catid) {
        ShopHttpConfig.FarmDownUpCarriage(context, type, catid, new Okhttputils.OkutListener() {
            @Override
            public void error(Call call, IOException e) {

            }

            @Override
            public void error(Call call, int code) {
                Log.e("上下架", "失败");
            }

            @Override
            public void suseff(final Call call, Response response, Object o) {
                Log.e("上下架", "成功");
                final FarmDownUpShop farmDownUpShop = (FarmDownUpShop) o;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (farmDownUpShop.getData().getStatus().equals("2")) {
                            Toast.makeText(context, "下架成功", Toast.LENGTH_SHORT).show();
                        } else if (farmDownUpShop.getData().getStatus().equals("3")) {
                            Toast.makeText(context, "上架成功", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }, FarmDownUpShop.class);
    }

    public interface DownUpCallBack {
        void callBack(String type);
    }

    class farmMyShopViewHolder {
        TextView my_farm_shop_item_date, my_farm_shop_item_name;
        TextView my_farm_shop_item_collect, my_farm_shop_item_goumai, my_farm_shop_item_look, my_farm_shop_item_remark;
        TextView my_farm_shop_item_cancel, my_farm_shop_item_hint;
        MyImageView my_farm_shop_item_imageView;
        ImageView my_farm_shop_item_bianji;

    }

}
